//
//  UserInfoViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-1-16.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "UserInfoViewController.h"
#import "CollectedViewController.h"

@interface UserInfoViewController ()

@end

@implementation UserInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotUserActions:) name:gotUserActionsNotify object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    usernameLabel.text = [[[AuthorManager defaultManager] userInfo] valueForKey:@"user_name"];
    emailLabel.text = [[[AuthorManager defaultManager] userInfo] valueForKey:@"email"];
    
    [[DataRequest sharedRequest] getUserActions];
}

- (void)gotUserActions:(NSNotification *)notification
{
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = [notification.object valueForKey:@"result"];
        int share = [[result valueForKey:@"share_count"] intValue];
        int comment = [[result valueForKey:@"commentt_count"] intValue];
        int collect = [[result valueForKey:@"favorite_count"] intValue];
        
        shareLabel.text = [NSString stringWithFormat:@"%d",share];
        commentLabel.text = [NSString stringWithFormat:@"%d",comment];
        collectLabel.text = [NSString stringWithFormat:@"%d",collect];
        
        shareArrow.hidden = (share == 0);
        commentArrow.hidden = (comment == 0);
        collectArrow.hidden = (collect == 0);
        
        shareButton.enabled = (share > 0);
        commentButton.enabled = (comment > 0);
        collectButton.enabled = (collect > 0);
    }
}

- (IBAction)logout:(id)sender
{
    [[AuthorManager defaultManager] logOut];
}

- (IBAction)showShared:(id)sender
{
    CollectedViewController *collectedViewController = [[[CollectedViewController alloc] initWithNibName:@"CollectedViewController" bundle:nil] autorelease];
    collectedViewController.actionType = UserShare;
    [self.navigationController pushViewController:collectedViewController animated:YES];
}

- (IBAction)showCommented:(id)sender
{
    CollectedViewController *collectedViewController = [[[CollectedViewController alloc] initWithNibName:@"CollectedViewController" bundle:nil] autorelease];
    collectedViewController.actionType = UserComment;
    [self.navigationController pushViewController:collectedViewController animated:YES];
}

- (IBAction)showCollected:(id)sender
{
    CollectedViewController *collectedViewController = [[[CollectedViewController alloc] initWithNibName:@"CollectedViewController" bundle:nil] autorelease];
    collectedViewController.actionType = UserCollect;
    [self.navigationController pushViewController:collectedViewController animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [usernameLabel release];
    [emailLabel release];
    [shareLabel release];
    [commentLabel release];
    [collectLabel release];
    [shareArrow release];
    [commentArrow release];
    [collectArrow release];
    [shareButton release];
    [commentButton release];
    [collectButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [usernameLabel release];
    usernameLabel = nil;
    [emailLabel release];
    emailLabel = nil;
    [shareLabel release];
    shareLabel = nil;
    [commentLabel release];
    commentLabel = nil;
    [collectLabel release];
    collectLabel = nil;
    [shareArrow release];
    shareArrow = nil;
    [commentArrow release];
    commentArrow = nil;
    [collectArrow release];
    collectArrow = nil;
    [shareButton release];
    shareButton = nil;
    [commentButton release];
    commentButton = nil;
    [collectButton release];
    collectButton = nil;
    [super viewDidUnload];
}
@end
